
	use "$temp\cleaned_obs_all.dta", clear

	* New: select years (+/- 1 year)
	keep if inrange(year, $syear , $eyear) // globals set in "Set globals"

	*merge with person-level sample identifier
	merge m:1 persnr using "$temp/pers_samp_id.dta"
	drop if _merge == 2
	keep if samp == $s
	drop samp
	drop _merge

	count
	count if pt == 0
	count if pt == 1

	count if spell_length < $drop_short

	count if            spell_length >= $drop_short
	count if pt == 0 &  spell_length >= $drop_short
	count if pt == 1 &  spell_length >= $drop_short

	su spell_length if spell_length >= $drop_short
	su spell_length if pt == 0 &  spell_length >= $drop_short
	su spell_length if pt == 1 &  spell_length >= $drop_short

	* Decide which spells to drop (globals are set in Master.do):
	* drop short spells and again combine spells within estabishment with gap <  $N_gap
	drop if spell_length < $drop_short

	sort persnr begorig
	gen gap = begorig-endorig[_n-1] - 1 if persnr==persnr[_n-1]
	assert gap >= 0 if !missing(gap)  
	su gap, de
	su gap if gap <  $N_gap
	su gap if gap >=  $N_gap, de

	* Create to id's
	sort persnr begorig
	gen long toid =.
	format toid %15.0f
	replace toid = betnr[_n+1] if gap[_n+1] <  $N_gap                & persnr==persnr[_n+1] 
	replace toid = 1           if gap[_n+1] >= $N_gap & gap[_n+1]!=. & persnr==persnr[_n+1]
	replace toid = 1           if                                      persnr!=persnr[_n+1] 
	label de transition_lab 1 "N", replace
	label val toid transition_lab
	assert !missing(toid)

	* Create "to-spell"
	gen long tospell =.
	format tospell %15.0f
	replace tospell = spell[_n+1] if gap[_n+1] <  $N_gap                & persnr==persnr[_n+1] 
	
	** Generate from id
	gen long fromid =.
	format fromid %15.0f
	replace fromid = betnr[_n-1] if gap <  $N_gap          & persnr==persnr[_n-1] 
	replace fromid = 1           if gap >= $N_gap & gap!=. & persnr==persnr[_n-1]
	replace fromid = 1           if                          persnr!=persnr[_n-1]
	label de transition_lab 1 "N", replace
	label val fromid transition_lab
	assert !missing(fromid)

	rename betnr currentid
	sort persnr begorig 	

	* Tag full-time to full-time moves, only keep ft-to-ft moves if not considering part-time
	gen tag_ft_pt = (gap[_n+1] <  $N_gap & persnr==persnr[_n+1] & pt == 0 & pt[_n+1] == 1)
	tab tag_ft_pt if pt == 0 & currentid!=toid & toid != 1 
	drop if tag_ft_pt == 1 & currentid!=toid & $drop_pt == 1 & toid != 1

	* Decide which spells to drop (globals are set in Master.do):
	drop if     pt == 1 &  $drop_pt   == 1 // drop part-time spells
	drop pt

	compress
	save "$temp\cleaned_obs_ver$ver.dta", replace

	recast double currentid toid fromid

	order fromid, after(persnr)
	order currentid, after(fromid)
	order toid, after(currentid)

	save "$temp/temp1", replace

	** Generate N to E observations
	keep persnr fromid currentid toid year begorig spell

	keep if fromid == 1
	replace toid = currentid
	replace currentid = fromid
	drop fromid
	replace year = year - 1 if day(begorig) == 1 &  month(begorig)  == 1
	drop begorig
	rename spell tospell
	save "$temp/temp2", replace

	use "$temp/temp1", clear
	drop fromid
	append using "$temp/temp2"
	erase "$temp/temp1.dta"
	erase "$temp/temp2.dta"
	sort persnr year currentid

	label val currentid transition_lab
	label val toid transition_lab

	compress
	save "$temp/cleaned_obs_reconfig_hockey_ver$ver.dta", replace


	*Keep movers only
	gen mover = (currentid!=toid)
	keep if mover==1
	drop mover

	gen m = month(endorig)
	gen d = day(endorig)
	gen q = ceil(m/3)
	replace year = year + 1 if d == 31 &  m == 12 
	replace q    = 1        if d == 31 &  m == 12   
	replace q    = q    + 1 if d == 31 &  m == 3            
	replace q    = q    + 1 if d == 30 & (m == 6 | m == 9)     
	drop m d

	* drop last spell that end in non-employment (retirement etc.)
	sort persnr begorig
	by persnr: gen tag1 = _n
	by persnr: gen tag2 = _N
	gen tag = (tag1==tag2 & toid == 1)
	tab tag, mis
	drop if tag==1
	drop tag tag1 tag2

	drop if year <= $syear 
	drop if year >= $eyear

	compress
	save "$temp/movers_ver$ver.dta", replace


	use currentid persnr year using "$temp/cleaned_obs_reconfig_hockey_ver$ver.dta", clear

	drop if year <= $syear 
	drop if year >= $eyear

	drop if currentid == 1
	duplicates drop
	gen workers = 1
	collapse (sum) workers, by(currentid year)
	compress
	save "$temp/workers_currentid_year_ver$ver.dta", replace
